package org.lifei.org.controller;

import org.lifei.base.annotation.LifPermission;
import org.lifei.org.service.IEmployeeService;
import org.lifei.org.domain.Employee;
import org.lifei.org.query.EmployeeQuery;
import org.lifei.base.util.AjaxResult;
import org.lifei.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.util.List;

@RestController
@RequestMapping("/employee")
@LifPermission(name = "员工管理",desc = "员工管理")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @LifPermission(name = "员工的新增或修改",desc = "新增or修改员工")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if( employee.getId()!=null)
                employeeService.update(employee);
            else
                employeeService.insert(employee);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @LifPermission(name = "员工删除",desc = "根据id删除员工")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Employee employee = employeeService.queryOne(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    @LifPermission(name = "查询素有员工",desc = "查询所有员工")
    public AjaxResult list(){

        try {
            List< Employee> list = employeeService.queryAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }
    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @LifPermission(name = "员工分页查询",desc = "员工分页查询")
    public AjaxResult json(@RequestBody EmployeeQuery query)
    {
        try {
            PageInfo<Employee> pageList = employeeService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    @PatchMapping
    @LifPermission(name = "员工批量删除",desc = "员工批量删除")
    public AjaxResult delete(@RequestBody List<String> ids)
    {
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！"+e.getMessage());
        }
    }


    @PostMapping("/upload")
    @LifPermission(name = "文件导入", desc = "通过文件导入用户信息")
    public AjaxResult upload(MultipartFile file) {
        try {
            return AjaxResult.success(employeeService.upload(file));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/download")
    @LifPermission(name = "导出数据", desc = "将用户信息列表导出为xlsx文件")
    public ResponseEntity<Resource> download(@RequestBody List<Employee> employees) {

        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            employeeService.download(employees, outputStream);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=fail.xlsx")
                    .body(new ByteArrayResource(outputStream.toByteArray()));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}
